<?php 
class Md_Search extends WdkModel
{
	public function __construct()
	{
		parent::__construct();
		$this->_init();
	}
	
	private function _init()
	{
		$this->table	= 'search';
		$this->order	= array();
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2011. 3. 25
	 */
	public function cLoad($search_code, $date='')
	{
		$date || $date = date('Y-m-d');
		return $this->getData("*", array("`search_code` = '{$this->escape($search_code)}'", "`search_date` = '{$date}'"));
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2011. 3. 25
	 */
	public function search_rank($num=3, $period=7)
	{
		$date = date('Y-m-d', mktime() - $period * 24 * 60 * 60);
		$sql = "SELECT
					`s`.`search_date`, `s`.`search_keyword`, SUM(`s`.`search_count`) AS `count` 
				FROM `{$this->table}` AS `s`
				WHERE `s`.`search_date` > '{$date}'
				GROUP BY `s`.`search_code`
				ORDER BY `count` DESC
				LIMIT 0, {$num}
				";
		$this->dbo->execute($sql);
		if($this->dbo->numRows() > 0)
		{
			return $this->dbo->rows();    		
		}
		throw new NoRecordException();
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2011. 3. 25
	 */
	public function hit($search_code='', $date='')
	{
		$date || $date = date('Y-m-d');
		$sql = "UPDATE 
					`{$this->table}`
				SET
					`search_count` = `search_count`+1
				WHERE `search_code` = '{$this->escape($search_code)}'
				AND	`search_date` = '{$date}'
				";
		$this->dbo->execute($sql);
	}
}